package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.result.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.controller.result.FindPrizeListResult;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@Slf4j
@RestController
public class PrizeController {
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private PictureService pictureService;

    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile picFile) {
        return pictureService.savePicture(picFile);
    }

    /**
     * 奖品创建
     * @param param
     * @param prizePic
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(
            @Validated @RequestPart("param") CreatePrizeParam param,
            @RequestPart("prizePic") MultipartFile prizePic) {
        // 日志打印
        log.info("PrizeController createPrice 接收到参数 param: {}, 文件: {}",
                JacksonUtil.writeValueAsString(param),
                prizePic.getOriginalFilename());
        // 业务处理
        return CommonResult.success(prizeService.createPrize(param, prizePic)); // 构造响应并返回
    }

    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPageList(PageParam param) {
        // 日志打印
        log.info("PrizeController findPageList 接收到参数 param: {}",
                JacksonUtil.writeValueAsString(param));
        // 业务处理
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPageList(param);
        return CommonResult.success(converToFindPrizeListResult(pageListDTO));
    }

    /**
     * 将 pageListDTO 类型转化为 FindPrizeListResult 类型
     * @param pageListDTO
     * @return
     */
    private FindPrizeListResult converToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (null == pageListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream().
                        map(prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }
}
